﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using PMIS.BLL;
using PMIS.BLL.Post;
using PMIS.BLL.User;

namespace PMIS.UI.User
{
	/// <summary>
	/// 概预算信息维护
	/// </summary>
	public partial class 概预算信息维护 : BaseForm
	{
		#region 私有变量
		/// <summary>
		/// 当前操作的记录
		/// </summary>
		private 其他_概预算 CurrentRecord = null;
		#endregion

		#region 单例
		/// <summary>
		/// 本类的实例
		/// </summary>
		private static 概预算信息维护 _form = null;
		/// <summary>
		/// 用于本类实现单例的Locker
		/// </summary>
		private static object locker = new object();
		/// <summary>
		/// 构造函数
		/// </summary>
		private 概预算信息维护()
		{
			InitializeComponent();
		}
		/// <summary>
		/// 获取本窗体的实例
		/// </summary>
		/// <param name="ID">记录ID</param>
		/// <param name="strUserID">工号</param>
		/// <returns></returns>
		public static 概预算信息维护 GetInstance(string ID, string strUserID)
		{
			if (_form == null || _form.IsDisposed)
			{
				lock (locker)
				{
					if (_form == null || _form.IsDisposed)
						_form = new 概预算信息维护();
				}
			}

			if (ID == "new")
			{
				_form.CurrentRecord = new 其他_概预算();
				_form.CurrentRecord.USER_ID = strUserID;
			}
			else
			{
				_form.CurrentRecord = Singleton<BL_概预算>.GetInstance().GetEntity(ID);
			}

			return _form;
		}
		#endregion

		#region 窗体事件
		/// <summary>
		/// 窗体载入事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void 概预算信息维护_Load(object sender, EventArgs e)
		{
			//	默认显示信息
			tabControl1.SelectedIndex = 0;
			//	更新界面
			UpdateFormView();
		}
		#endregion

		#region 界面显示
		/// <summary>
		/// 更新界面显示
		/// </summary>
		private void UpdateFormView()
		{
			if (CurrentRecord == null)
				return;

			try
			{
				//	岗位等级
				cbLevel.BeginUpdate();
				cbLevel.DataSource = Singleton<BL_岗位_分类_等级>.GetInstance().GetDetails("概预算");
				//	等级
				cbLevel.Text = CurrentRecord.等级;
				cbLevel.EndUpdate();

				//	操作证号
				tb证书编号.Text = CurrentRecord.证书编号;
				//	发证时间
				dtp发证时间.Value = CurrentRecord.发证时间.HasValue ? CurrentRecord.发证时间.Value : DateTime.Now;
				dtp发证时间.Checked = CurrentRecord.发证时间.HasValue;
				//	有效期
				dtp有效期.Value = CurrentRecord.有效期.HasValue ? CurrentRecord.有效期.Value : DateTime.Now;
				dtp有效期.Checked = CurrentRecord.有效期.HasValue;
				//	备注
				tb备注.Text = CurrentRecord.备注;

				//	扫描件
				if (CurrentRecord.证书扫描件 != null)
				{
					using (MemoryStream ms = new MemoryStream(CurrentRecord.证书扫描件))
					{
						ms.Position = 0;
						pictureBox.Image = CreateThumailImage(Image.FromStream(ms), pictureBox.Width - 2, pictureBox.Height - 2);
					}
				}
				else
				{
					pictureBox.Image = null;
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion

		#region 触发事件
		/// <summary>
		/// 检查输入
		/// </summary>
		/// <returns></returns>
		private bool CheckInput()
		{
			if (string.IsNullOrEmpty(tb证书编号.Text.Trim()))
			{
				ShowErrorMessage("证书编号不能为空!");
				tb证书编号.Focus();
				return false;
			}

			if (string.IsNullOrEmpty(cbLevel.Text.Trim()))
			{
				ShowErrorMessage("等级不能为空!");
				cbLevel.Focus();
				return false;
			}

			return true;
		}
		/// <summary>
		/// 保存
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btSave_Click(object sender, EventArgs e)
		{
			if (!CheckInput())
				return;

			try
			{
				//	证书编号
				CurrentRecord.证书编号 = tb证书编号.Text.Trim();
				//	初领时间
				CurrentRecord.发证时间 = dtp发证时间.Checked ? (DateTime?)dtp发证时间.Value : null;
				//	有效期
				CurrentRecord.有效期 = dtp有效期.Checked ? (DateTime?)dtp有效期.Value : null;
				//	岗位等级
				CurrentRecord.岗位等级ID = (cbLevel.SelectedItem as 岗位_分类_等级).ID;
				//	等级
				CurrentRecord.等级 = cbLevel.Text.Trim();
				//	备注
				CurrentRecord.备注 = tb备注.Text.Trim();

				//	保存
				if (Singleton<BL_概预算>.GetInstance().Save(CurrentRecord))
				{
					ShowSuccessMessage("保存成功!");
					DialogResult = DialogResult.OK;
					this.Close();
				}
				else
				{
					ShowErrorMessage("保存记录时发生错误!");
					DialogResult = DialogResult.Cancel;
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 取消
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btCancel_Click(object sender, EventArgs e)
		{
			DialogResult = DialogResult.Cancel;
			this.Close();
		}
		/// <summary>
		/// 选择照片
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btSelectPicture_Click(object sender, EventArgs e)
		{
			try
			{
				//	默认从我的文档选择照片
				openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
				//	过滤器
				openFileDialog.Filter = "所有支持的图像格式 (*.jpg;*.jpeg;*.bmp;*.png;*.gif)|*.jpg;*.jpeg;*.bmp;*.png;*.gif";

				if (openFileDialog.ShowDialog(this) == DialogResult.OK)
				{
					//	显示选择的图片
					using (Image img = Image.FromFile(openFileDialog.FileName))
					{
						//	显示缩放后的图片
						pictureBox.Image = CreateThumailImage(img, pictureBox.Width - 2, pictureBox.Height - 2);

						using (MemoryStream ms = new MemoryStream())
						{
							img.Save(ms, ImageFormat.Jpeg);
							CurrentRecord.证书扫描件 = ms.ToArray();
						}
					}
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 保存照片
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btSavePicture_Click(object sender, EventArgs e)
		{
			if (CurrentRecord == null || CurrentRecord.证书扫描件 == null)
				return;

			try
			{
				//	默认从我的文档选择照片
				saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
				//	过滤器
				saveFileDialog.Filter = "jpg文件(*.jpg)|*.jpg";
				//	默认文件名
				saveFileDialog.FileName = string.Empty;

				if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
				{
					using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write, FileShare.None))
					{
						fs.Write(CurrentRecord.证书扫描件, 0, CurrentRecord.证书扫描件.Length);
						fs.Flush();
						fs.Close();
					}
					//	启动图片查看程序显示图片
					Process.Start(saveFileDialog.FileName);
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion
	}
}
